CROSS_COMPILE	=	aarch64-linux-gnu-gcc

CC		=	$(CROSS_COMPILE)

 
DIRS = $(shell find . -type d)

 
CFILES_DIR 	= $(shell find . -type f -name "*.c")

CFILES 		= $(notdir $(CFILES_DIR))

# 为每个.c文件生成对应的可执行文件
TARGETS = $(CFILES:.c=)

HFILES_DIR 	= $(shell find . -type f -name "*.h")

HFILES 		= $(notdir $(HFILES_DIR))

HDIRS		= $(sort $(dir $(HFILES_DIR)))

 

AFILES_DIR 	= $(shell find . -type f -name "lib*.a")

AFILES		= $(notdir $(AFILES_DIR))

ADIRS		= $(sort $(dir $(AFILES_DIR)))

 

SOFILES_DIR = $(shell find . -type f -name "lib*.so")

SOFILES		= $(notdir $(SOFILES_DIR))

SODIRS		= $(sort $(dir $(SOFILES_DIR)))


INCLUDES += $(HDIRS:%=-I%)


$(warning INCLUDES : [ $(INCLUDES) ])
 
CFLAGS 	+= $(INCLUDES)


CFLAGS 	+= -Wall -Werror


CFLAGS 	+= -g -MD -O2 -static


LDFLAGS += $(ADIRS:%=-L%)

LDFLAGS += $(SODIRS:%=-L%)

 
LDFLAGS += $(AFILES:lib%.a=-l%)

LDFLAGS += $(SOFILES:lib%.so=-l%)

 
LDFLAGS += -lpthread

 
$(warning LDFLAGS : [ $(LDFLAGS) ])


VPATH = $(DIRS)


$(warning TARGETS : [ $(TARGETS) ])
$(warning CFILES : [ $(CFILES) ])
 
# 为每个目标生成对应的对象文件
OBJS = $(foreach target,$(TARGETS),obj/$(target).o)

DEPS 	= $(foreach target,$(TARGETS),obj/$(target).d)

 
###########################################################

 
.PHONY : all clean cleanall $(TARGETS)

 
all : $(TARGETS)

# 为每个目标生成对应的可执行文件
$(TARGETS): % : obj/%.o
	@echo
	@echo "Linking $@..."
	@echo
	$(CC) $< -o $@ $(LDFLAGS)
	@echo
	@echo " < $@ > is OK!"
	@echo

 
obj/%.o : %.c
	@mkdir -p obj
	$(CC) $(CFLAGS) -c $< -o $@
 

clean :
	rm -rf $(OBJS) $(DEPS) obj

 
cleanall :
	rm -rf $(OBJS) $(DEPS) obj $(TARGETS)

 

sinclude $(DEPS)
